package com.info.zhiduoduo.admin.service;

import com.alibaba.druid.util.StringUtils;
import com.info.zhiduoduo.admin.model.formbean.customer.QueryFollowingFormBean;
import com.info.zhiduoduo.admin.model.formbean.customer.SaveFollowingFormBean;
import com.info.zhiduoduo.admin.model.formbean.customer.UpdateFollowingFormBean;
import com.info.zhiduoduo.common.constants.SmartResult;
import com.info.zhiduoduo.common.constants.StatusMsg;
import com.info.zhiduoduo.common.formbean.BaseSimpleFormBean;
import com.info.zhiduoduo.core.service.WxMessageService;
import com.info.zhiduoduo.core.service.common.BaseService;
import com.info.zhiduoduo.repository.dto.customer.FollowingDto;
import com.info.zhiduoduo.repository.entity.customer.Following;
import com.info.zhiduoduo.repository.entity.customer.ServiceError;
import com.info.zhiduoduo.repository.mapper.customer.FollowingMapper;
import com.info.zhiduoduo.repository.mapper.customer.ServiceErrorMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.Date;
import java.util.List;

@Service
public class FollowingService extends BaseService {
    @Autowired
    FollowingMapper followingMapper;
    @Autowired
    private ServiceErrorMapper serviceErrorMapper;
    @Autowired
    private WxMessageService wxMessageService;

    public SmartResult saveFollowing(BaseSimpleFormBean<SaveFollowingFormBean> formBean) {
        if (null == formBean || null == formBean.getReqdata() || StringUtils.isEmpty(formBean.getReqdata().getFollowingRow())) {
            return errorParamSmartResult();
        }
        Following following = new Following();
        BeanUtils.copyProperties(formBean.getReqdata(), following);
        following.setCreatePerson(formBean.getUserid());
        following.setUpdatePerson(formBean.getUserid());
        if (!following.insert()) {
            return smartResult(StatusMsg.StatusMsg_199);
        }
        //发微信通知
        Integer id = formBean.getReqdata().getServiceErrorID();
        ServiceError serviceError = serviceErrorMapper.selectById(id);
        boolean messageFlag = wxMessageService.sendNoticeToHuGangForServiceError(serviceError.getContent(), 3, serviceError.getWheatherSolve(), serviceError.getInvolvePerson(), serviceError.getFollowPerson(),formBean.getReqdata().getFollowingRow());
        if (!messageFlag) {
            return smartResult(StatusMsg.StatusMsg_199);
        }

        return smartResult(following);
    }

    public SmartResult updateFollowing(BaseSimpleFormBean<UpdateFollowingFormBean> formBean) {
        if (null == formBean || null == formBean.getReqdata() || formBean.getUserid()==0 || StringUtils.isEmpty(formBean.getReqdata().getFollowingRow())) {
            return errorParamSmartResult();
        }
        UpdateFollowingFormBean updateFollowingFormBean = formBean.getReqdata();
        Following following = new Following().selectById(updateFollowingFormBean.getId());
        if (null == following) {
            return smartResult(StatusMsg.StatusMsg_102);
        }
        //COPY传回的参数
        BeanUtils.copyProperties(updateFollowingFormBean, following);
        following.setUpdatePerson(formBean.getUserid());
        following.setUpdatetime(new Date());
        if (!following.updateById()) {
            return smartResult(StatusMsg.StatusMsg_199);
        }
        //发微信通知

        Integer id = followingMapper.selectById(formBean.getReqdata().getId()).getServiceErrorID();
        ServiceError serviceError = serviceErrorMapper.selectById(id);
        boolean messageFlag = wxMessageService.sendNoticeToHuGangForServiceError(serviceError.getContent(), 3, serviceError.getWheatherSolve(), serviceError.getInvolvePerson(), serviceError.getFollowPerson(),formBean.getReqdata().getFollowingRow());
        if (!messageFlag) {
            return smartResult(StatusMsg.StatusMsg_199);
        }
        return smartResult(following);
    }

    public SmartResult findFollowingByRelationId(BaseSimpleFormBean<QueryFollowingFormBean> formBean) {
        if (formBean == null || formBean.getReqdata() == null || formBean.getReqdata().getServiceErrorId() == null) {
            return errorParamSmartResult();
        }
        List<FollowingDto> followingByRelationId = followingMapper.findFollowingByRelationId(formBean.getReqdata().getServiceErrorId());
        return smartResult(followingByRelationId);
    }
}
